import 'dart:io';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:friends_flutter/config/widgets/xd_activity_indicator.dart';

import '../../utils/xd_file_utils.dart';

showImageListWithURL(
    BuildContext context, List dataList, int index, String tagId,
    {bool showDot = true, String? dynamicId}) {
  Navigator.push(
      context,
      PageRouteBuilder(
          //打开动画时间
          transitionDuration: const Duration(milliseconds: 300),
          //关闭动画时间
          reverseTransitionDuration: const Duration(milliseconds: 300),
          pageBuilder: (BuildContext context, Animation animation,
              Animation secondaryAnimation) {
            return FadeTransition(
                opacity: animation as Animation<double>,
                child: Scaffold(
                  backgroundColor: Colors.black,
                  body: NinePictureAllScreenShow(
                    dataList,
                    index,
                    showDot,
                    tagId,
                    dynamicId: dynamicId,
                  ),
                ));
          }));
}

///图片全屏展示
class NinePictureAllScreenShow extends StatelessWidget {
  final List picList;
  int initIdx;
  final bool showDot;
  final String tagId;
  String? dynamicId;
  final List sendList = [];

  ExtendedPageController? _controller;

  NinePictureAllScreenShow(this.picList, this.initIdx, this.showDot, this.tagId,
      {Key? key, this.dynamicId = ''})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    // scaleStateController.scaleState = PhotoViewScaleState.covering;
    _controller = ExtendedPageController(initialPage: initIdx);

    return Container(
      color: Colors.black,
      height: double.infinity,
      width: double.infinity,
      // padding: const EdgeInsets.fromLTRB(0, 0, 0, 30),
      alignment: Alignment.center,
      child: StatefulBuilder(builder: (BuildContext context, StateSetter setState){
        return _bannerRoomWidget(context,setState);
      },
      ),
    );
  }


  //照片图
  Widget _bannerRoomWidget(context,setState) {
    List<Widget> list = [];
    for(int i = 0;i < picList.length;i++){
      list.add(Container(
        width: 8,
        height: 8,
        margin: const EdgeInsets.symmetric(horizontal: 4,vertical: 20),
        decoration: BoxDecoration(
          color: i == initIdx ? const Color(0xFFFFFFFF) : const Color(0x7FFFFFFF),
          borderRadius: BorderRadius.circular(10),
        ),
      ));
    }
    return Container(
        width: double.infinity,
        child: Stack(
          children: [
            ExtendedImageGesturePageView.builder(
              controller: _controller,
              onPageChanged: (value){
                initIdx = value;
                setState((){});
              },
              itemBuilder: (BuildContext context, int index) {
                if (!sendList.contains(picList[index]) &&
                    picList[index] is String) {
                  //上传互动数据
                  // Api().dynamicInteraction(dynamicId ?? '', 5, picList[index]);
                  sendList.add(picList[index]);
                }

                ImageProvider imageProvider;
                if (picList[index].toString().startsWith('http')) {
                  imageProvider = NetworkImage(picList[index]);
                } else {
                  if (picList[index] is String) {
                    imageProvider = FileImage(File(picList[index]));
                  } else {
                    imageProvider = FileImage(picList[index]);
                  }
                }

                return GestureDetector(
                  child: Hero(
                    tag: picList.length > 1 ? (tagId + index.toString()) : tagId,
                    child: ExtendedImage(
                      image: imageProvider,
                      fit: BoxFit.contain,
                      mode: ExtendedImageMode.gesture,
                      // onDoubleTap: widget.delegate.updateAnimation,
                      initGestureConfigHandler: (ExtendedImageState state) {
                        return GestureConfig(
                          initialScale: 1.0,
                          minScale: 1.0,
                          maxScale: 4.0,
                          animationMinScale: 0.6,
                          animationMaxScale: 4.0,
                          cacheGesture: false,
                          inPageView: true,
                        );
                      },
                      loadStateChanged: (ExtendedImageState state) {
                        if (state.extendedImageLoadState == LoadState.loading) {
                          return const XdCupertinoActivityIndicator();
                        }
                        if (state.extendedImageLoadState == LoadState.failed) {
                          return Image.asset(
                            'assets/images/news/news_msg_img_placeholder.png',
                            height: 300,
                            width: 300,
                          );
                        }
                      },
                    ),
                  ),
                  onLongPress: () {
                    _showBottomSheet(picList[index], context);
                  },
                  onTap: () {
                    Navigator.pop(context);
                  },
                );
              },
              itemCount: picList.length,
            ),
            Align(
              alignment: Alignment.bottomCenter,
              child: Offstage(
                offstage: picList.length <= 1 || !showDot,
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.center, children: list),
              ),
            )
          ],
        ));
  }

  //底部弹窗
  void _showBottomSheet(String imgUrl, context) {
    showModalBottomSheet(
      backgroundColor: Colors.transparent,
      isDismissible: true,
      context: context,
      builder: (BuildContext bc) {
        return StatefulBuilder(
          builder: (context, state) {
            return Container(
//              color: Colors.white,
              height: 150,
              child: Stack(
                children: <Widget>[
                  Container(
                    height: 30.0,
                    width: double.infinity,
                    color: Colors.transparent,
                  ),
                  Container(
                    decoration: const BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(25),
                        topRight: Radius.circular(25),
                      ),
                    ),
                  ),
                  Column(
                    children: [
                      InkWell(
                        child: Container(
                          height: 40,
                          width: double.infinity,
                          margin: const EdgeInsets.only(top: 20),
                          alignment: Alignment.center,
                          child: const Text('保存到本地',
                              style:
                                  TextStyle(color: Colors.black, fontSize: 16)),
                        ),
                        onTap: () {
                          FileUtils.saveImg(imgUrl, true).then((value) {
                            Navigator.pop(context);
                          });
                        },
                      ),
                      InkWell(
                        child: Container(
                          height: 50,
                          width: double.infinity,
                          alignment: Alignment.center,
                          decoration: const BoxDecoration(
                            color: Color(0xFFF2F2F2),
                            borderRadius: BorderRadius.all(Radius.circular(15)),
                          ),
                          margin: const EdgeInsets.all(20),
                          child: const Text('取消',
                              style:
                                  TextStyle(color: Colors.black, fontSize: 16)),
                        ),
                        onTap: () {
                          Navigator.pop(context);
                        },
                      ),
                    ],
                  )
                ],
              ),
            );
          },
        );
      },
    );
  }
}
